package com.ag.work.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.ag.bean.TbDisposalAdvice;
import com.ag.common.IDUtil;
import com.ag.common.IIDService;
import com.ag.common.PageUtil;
import com.ag.work.dao.MyAdviceDAO;
import com.ag.work.dao.WorkUtilDAO;
import com.ag.work.service.IMyAdivceService;
import com.sun.org.apache.commons.logging.Log;
import com.sun.org.apache.commons.logging.LogFactory;

public class MyAdviceServiceImpl implements IMyAdivceService {
	private static final Log log = LogFactory.getLog(MyAdviceServiceImpl.class);
	private MyAdviceDAO myAdviceDAO;	
	private WorkUtilDAO workUtilDAO;
	@Resource(name = "iDServiceImpl")
	private IIDService idServiceImpl;

	/**
	 *查找我的处置建议
	 */
	@Override
	public List<TbDisposalAdvice> findMyAdvice(String UId) {
		return myAdviceDAO.findMyAdvice(UId);
	}
	/**
	 *查找我的处置建议1
	 */
	public List findMyAdvice1(String UId){
		return myAdviceDAO.findMyAdvice1(UId);
	}
	/**
	 *查找我的建议申请有分页功能
	 */
	public PageUtil findRequestPage(int total, int page, int maxRow, String UId){
		String sql = "select count(distinct da.daId) from TbDisposalAdvice da where 1 = 1";
		String hql = "from TbDisposalAdvice da left join fetch da.tbUserByApplyUId " +
				"left join fetch da.tbUserByAdviceUId left join fetch da.tbEvent" +
				" where 1 = 1";
		
		if (UId!=null&&UId.length() > 0){
			sql += " and da.tbUserByApplyUId.UId = '" + UId + "'";
			hql += " and da.tbUserByApplyUId.UId = '" + UId + "'";
		}
		//hql = "order by da.c desc";
		PageUtil pageUtil = new PageUtil();
		List result = null;
		try {
			total = ((Long) this.workUtilDAO.queryCount(sql).iterator().next()).intValue();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		System.out.println("输出总条数："+total);
		if (page == 0) {
			page = 1;
		}
		if (maxRow == 0) {
			maxRow = 10;
		}
		int firstRow = (page - 1) * maxRow;
		System.out.println("------------------------------------");		
		try {
			result = workUtilDAO.queryList(hql, firstRow, maxRow);
	
		System.out.println("result  = " + result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		pageUtil.setTotal(total);
		pageUtil.setPage(page);
		pageUtil.setMaxRow(maxRow);
		pageUtil.setList(result);
		
		return pageUtil;
	}
	/**
	 *查找我的处置建议有分页功能
	 */
	public PageUtil findAdvicePage(int total, int page, int maxRow, String UId){
		String sql = "select count(distinct da.daId) from TbDisposalAdvice da where 1 = 1";
		String hql = "from TbDisposalAdvice da left join fetch da.tbUserByApplyUId " +
				"left join fetch da.tbUserByAdviceUId left join fetch da.tbEvent" +
				" where 1 = 1";
		
		if (UId!=null&&UId.length() > 0){
			sql += " and da.tbUserByAdviceUId.UId = '" + UId + "'";
			hql += " and da.tbUserByAdviceUId.UId = '" + UId + "'";
		}
		//hql = "order by da.c desc";
		PageUtil pageUtil = new PageUtil();
		List result = null;
		try {
			total = ((Long) this.workUtilDAO.queryCount(sql).iterator().next()).intValue();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		System.out.println("输出总条数："+total);
		if (page == 0) {
			page = 1;
		}
		if (maxRow == 0) {
			maxRow = 10;
		}
		int firstRow = (page - 1) * maxRow;
		System.out.println("------------------------------------");		
		try {
			result = workUtilDAO.queryList(hql, firstRow, maxRow);
	
		System.out.println("result  = " + result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		pageUtil.setTotal(total);
		pageUtil.setPage(page);
		pageUtil.setMaxRow(maxRow);
		pageUtil.setList(result);
		
		return pageUtil;
	}
	/**
	 *查找我的建议申请
	 */
	@Override
	public List findMyRequest(String UId) {
		return myAdviceDAO.findRequest(UId);
	}
	/**
	 *更新我的处置建议
	 */
	@Override
	public boolean update(TbDisposalAdvice disposalAdvice) {
		//log.info("update    daId----"+disposalAdvice.getDaId());
		myAdviceDAO.attachDirty(disposalAdvice);
		return false;
	}
	/**
	 *通过daId查找处置建议
	 */
	public TbDisposalAdvice findByDaId(String daId){
		log.info("findByDaId      daId-----"+daId);
		log.info("对象:"+this.myAdviceDAO.findById(daId));
		return this.myAdviceDAO.findById(daId);
	}
	/**
	 *用UId去获取当前用户对象
	 */
	/*public TbUser findById(String UId){
		log.info("UId:-------"+UId);
		log.info("对象："+this.tbUserDAO.findById(UId));
		return this.tbUserDAO.findById(UId);
	}*/
    /**
     *保存处置建议（未用）
     */
	public boolean save(TbDisposalAdvice disposalAdvice){
		disposalAdvice.setDaId(idServiceImpl.findTablePK(IDUtil.TB_DISPOSAL_ADVICE));
		disposalAdvice.setDaApplyTime(new Date());
		disposalAdvice.setA(IDUtil.PRESERVE);
		this.myAdviceDAO.save(disposalAdvice);
		return false;
	}
	
	public MyAdviceDAO getMyAdviceDAO() {
		return myAdviceDAO;
	}

	public void setMyAdviceDAO(MyAdviceDAO myAdviceDAO) {
		this.myAdviceDAO = myAdviceDAO;
	}
	public WorkUtilDAO getWorkUtilDAO() {
		return workUtilDAO;
	}
	public void setWorkUtilDAO(WorkUtilDAO workUtilDAO) {
		this.workUtilDAO = workUtilDAO;
	}

	

	
}
